package com.nowcoder.community.service.impl;

import com.nowcoder.community.service.ILikeService;
import com.nowcoder.community.util.RedisKeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.stereotype.Service;

/**
 * @author cjh
 * @date 2023/1/11 - 20:33
 */
@Service
public class LikeServiceImpl implements ILikeService {
    @Autowired
    private RedisTemplate template;

    /**
     * @param userId       点赞人的id
     * @param entityType   点赞对象的类型
     * @param entityId     点赞对象的id
     * @param entityUserId 点赞对象的创建用户的id
     */
    @Override
    public void like(int userId, int entityType, int entityId, int entityUserId) {
//        String entityLikeKey = RedisKeyUtil.getEntityLikeKey(entityType, entityId);
//        Boolean isMember = template.opsForSet().isMember(entityLikeKey, userId);
//        if (isMember) {
//            template.opsForSet().remove(entityLikeKey, userId);
//        } else {
//            template.opsForSet().add(entityLikeKey, userId);
//        }

        template.execute(new SessionCallback() {
            @Override
            public Object execute(RedisOperations operations) throws DataAccessException {
                String entityLikeKey = RedisKeyUtil.getEntityLikeKey(entityType, entityId);
                String userLikeKey = RedisKeyUtil.getUserLikeKey(entityUserId);
//              把查询梵高执行事务外面
                Boolean isMember = template.opsForSet().isMember(entityLikeKey, userId);

                operations.multi();
                if (isMember) {
                    operations.opsForSet().remove(entityLikeKey, userId);
                    operations.opsForValue().decrement(userLikeKey);
                } else {
                    operations.opsForSet().add(entityLikeKey, userId);
                    operations.opsForValue().increment(userLikeKey);
                }

                return operations.exec();
            }
        });

    }

    /**
     * 查询某实体点赞的数量
     *
     * @param entityType
     * @param entityId
     * @return
     */
    @Override
    public long findEntityLikeCount(int entityType, int entityId) {
        String entityLikeKey = RedisKeyUtil.getEntityLikeKey(entityType, entityId);
        Long size = template.opsForSet().size(entityLikeKey);
        return size==null?0:size.longValue();
    }

    /**
     * 查询某人对某实体的点赞状态
     *
     * @param userId
     * @param entityType
     * @param entityId
     * @return
     */
    @Override
    public int findEntityLikeStatus(int userId, int entityType, int entityId) {
        String entityLikeKey = RedisKeyUtil.getEntityLikeKey(entityType, entityId);
        return template.opsForSet().isMember(entityLikeKey, userId) ? 1 : 0;
    }

    /**
     * 查询某个用户获得的赞
     *
     * @param userId
     * @return
     */
    @Override
    public int findUserLikeCount(int userId) {
        String key = RedisKeyUtil.getUserLikeKey(userId);
        Integer count = (Integer) template.opsForValue().get(key);

        return count == null ? 0 : count.intValue();
    }
}
